<?php
namespace app\index\controller;
use think\Controller;
use think\Log;
use think\Db;
use think\Config;
use think\Url;
use think\Request;
class Base extends Controller
{

    protected $user;
    protected function checkLogin() {
        if ($this->loginStatus()) {
            return;
        }
        else {
            $this->clearCookie();
            $this->error("您长时间未操作，请重新登录！", "Index/index/index");
        }
    }

    private function loginStatus() {
        if (isset($_COOKIE['user']['id'])) {
            $this->user = Db::name("user")->where("id", $_COOKIE['user']['id'])->find();
            if ($this->user && $this->checkCookie($this->user['passwd'])) {
                if ((time() - $_COOKIE['user']['login_time']) > 300) {
                    $this->setCookie($this->user);
                }
                $this->assign('user',$this->user);
                return True;
            }
        }
        return false;
    }

    protected function getPwd($passwd) {
        $salt = "0000000000000000";
        $temp = sha1($salt . strtoupper($passwd), true);
        $password = strtoupper(bin2hex($salt.$temp));
        return $password;
    }

    protected function validParam($param, $type) {
        $type = strtolower($type);
        switch($type) {
            case "phone": {
                $pattern = '/^1([0-9]{10})$/';
                $error = "手机号格式错误！";
                break;
            }
            case "password": {
                $pattern = '/^[_0-9a-zA-Z]{6,16}$/';
                $error = "密码必须是由下划线，数字，字母组成的6-16位字符！";
                break;
            }
            case "email": {
                $pattern = '/^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$/';
                $error = "邮箱格式错误！";
                break;
            }
            default: {
                $this->error("参数错误！");
                break;
            }
        }
        if(preg_match($pattern, $param)){
            return;
        }
        else {
            $this->error($error);
        }
    }

    protected function setCookie($info) {
        $expired = 3600;
        $last_time = time();
        $key = md5($info['id'] . $info['phone'] . $info['passwd'] . $last_time);
        cookie('user[id]', $info['id'], $expired);
        cookie('user[phone]', $info['phone'], $expired);
        cookie('user[login_time]', $last_time, $expired);
        cookie('user[key]', $key, $expired);
    }


    protected function clearCookie() {
        $_SESSION['login_type'] ='';
        cookie('access_token',null);
        cookie('user[id]',null);
        cookie('user[phone]',null);
        cookie('user[login_time]',null);
        cookie('user[key]',null);
    }

    protected function checkCookie($passwd) {
        if(isset($_COOKIE['user'])){
            $key=$_COOKIE['user']['key'];
            $now_key=$_COOKIE['user']['id'] . $_COOKIE['user']['phone'] . $passwd . $_COOKIE['user']['login_time'];
            if($key!=md5($now_key))
                return false;
            return true;
        }else{
            return false;
        }
    }


    /**
     * @param string $code
     * @param null $msg
     * @param null $data
     * @return mixed
     */
    protected function buildResponse($code, $msg = null, $data = null) {

        if ('000000' == $code) {
            $response['result'] = TRUE;
        } else {
            $response['result'] = FALSE;
        }
        $response['msg'] = $msg;
        $response['code'] = $code;
        $response['data'] = $data;
        return $response;
    }

}
